package io.babystar.modules.business.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.babystar.common.exception.RRException;
import io.babystar.common.utils.ShiroUtils;
import io.babystar.modules.business.entity.BaseChannelEntity;
import io.babystar.modules.oss.cloud.OSSFactory;
import io.babystar.modules.sys.controller.AbstractController;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.babystar.modules.business.entity.TeacherEntity;
import io.babystar.modules.business.service.TeacherService;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 *
 *
 * @author jeson
 * @email 574197743@qq.com
 * @date 2021-03-23 20:03:34
 */
@RestController
@RequestMapping("business/teacher")
public class TeacherController extends AbstractController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("business:teacher:list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity user = this.getUser();
        params.put("organizeId",user.getBusinessId());
        int type = user.getType();
        if(type==2 || type==3){
            long teacherId = user.getParentTeacherId();
            TeacherEntity teacherEntity = teacherService.getById(teacherId);
            params.put("shopId",teacherEntity.getShopId());
        }
        PageUtils page = teacherService.queryPageTeacher(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("business:teacher:info")
    public R info(@PathVariable("id") Long id){
		TeacherEntity teacher = teacherService.getById(id);

        return R.ok().put("teacher", teacher);
    }


    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:teacher:save")
    public R save(@RequestBody TeacherEntity teacher){
        if(StringUtils.isNotBlank(teacher.getPassword())) {
            //String salt = "XicP18901KbQu6mQ7AZk";
            //teacher.setPassword(new Sha256Hash(teacher.getPassword(), salt).toHex());
            teacher.setPassword(teacher.getPassword());
        }
        teacherService.saveTeacher(teacher,this.getUser());
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:teacher:update")
    public R update(@RequestBody TeacherEntity teacher){
        if(StringUtils.isNotBlank(teacher.getPassword())) {
            //String salt = "XicP18901KbQu6mQ7AZk";
            //teacher.setPassword(new Sha256Hash(teacher.getPassword(), salt).toHex());
            teacher.setPassword(teacher.getPassword());
        }
        teacherService.updateById(teacher);

        return R.ok();
    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("business:teacher:delete")
    public R delete(@RequestBody Long[] ids){
		teacherService.delete(ids);

        return R.ok();
    }


    /**
     * 上传图片，返回url（公共方法接口）系统配置七牛
     */
    @PostMapping("/uploadPicture")
    public R uploadPicture(@RequestParam("file") MultipartFile file, String type) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }

        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        return R.ok().put("url", url).put("type", type);
    }

    /**
     * 获取机构/店铺下的老师
     */
    @RequestMapping("/getTeacherList")
    public R getTeacherList(){
        SysUserEntity user = this.getUser();
        Map<String, Object> map = new HashMap<>();
        map.put("organizeId",user.getBusinessId());
        int type = user.getType();
        if(type==2 || type==3){
            long teacherId = user.getParentTeacherId();
            TeacherEntity teacherEntity = teacherService.getById(teacherId);
            map.put("shopId",teacherEntity.getShopId());
        }
        List<TeacherEntity> teacherList = teacherService.getTeacherList(map);
        return R.ok().put("list", teacherList);
    }
}
